﻿using CPPEI.ETL.DataServiceInterface;
using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using SER = CPPEI.ETL.Domain.Services;

namespace CPPEI.ETL.WebApi
{
    [CPPEI.WebApi.AuthPostAction("ETL", "UpdateDataSourceColumns", "1.0")]
    public class UpdateDataSourceColumnsAction : BaseAction<UpdateDataSourceColumnsParams, UpdateDataSourceColumnsResult>
    {
        protected override ActionResult<UpdateDataSourceColumnsResult> OnExecute(ActionContext context, 
            SysParameters sysParameters,
            UpdateDataSourceColumnsParams opParams)
        {
            SER.DtSerService service = new SER.DtSerService();
            ServiceResult serResult = service.UpdateDataSourceColumns(context.UserId, opParams.ServiceId, opParams.SourceId, opParams.ColumnSettings);

            UpdateDataSourceColumnsResult result = new UpdateDataSourceColumnsResult();

            if (serResult.IsSuccess)
            {
                return new ActionResult<UpdateDataSourceColumnsResult>(result);
            }
            else
            {
                return new ActionResult<UpdateDataSourceColumnsResult>(serResult.IsSuccess, serResult.ErrorCode, serResult.Message, null);
            }
        }
    }

    public class UpdateDataSourceColumnsParams
    {
        [Parameter]
        public Guid ServiceId { get; set; }

        [Parameter]
        public Guid SourceId { get; set; }

        [Parameter]
        public string ColumnSettings { get; set; }
    }

    public class UpdateDataSourceColumnsResult
    {
        public List<Item> Items = new List<Item>();

        public class Item
        {
            public string Name { get; set; }
            public string Type { get; set; }
        }
    }
}
